package com.tjsinfo.tjsoa.articles.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.jdbc.core.RowMapper;

import com.tjsinfo.tjsoa.Dao.IHQLDao;
import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.articles.vo.TSummaryInfo;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.ToolKit;

public class SummaryBO implements ISummaryBO {

	private IQbcDao qbcDao;
	private IHibernateDao hibernateDao;
	private IJDBCDao jdbcDao;
	private IHQLDao hqlDao;
	private ISpringDao springDao;
	
	
	
	public ISpringDao getSpringDao() {
		return springDao;
	}


	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}


	public IQbcDao getQbcDao() {
		return qbcDao;
	}


	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}


	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}


	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}


	public IJDBCDao getJdbcDao() {
		return jdbcDao;
	}


	public void setJdbcDao(IJDBCDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}


	public IHQLDao getHqlDao() {
		return hqlDao;
	}


	public void setHqlDao(IHQLDao hqlDao) {
		this.hqlDao = hqlDao;
	}
	
	public PaginationSupport findBySearch(String sstart, String slimit,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	public Integer addSummary(TSummaryInfo summaryInfo) {
		// TODO Auto-generated method stub
		return   hibernateDao.save(summaryInfo);
	}

	public TSummaryInfo getSummaryId(Integer id) {
		// TODO Auto-generated method stub
		return  (TSummaryInfo) hibernateDao.findById(TSummaryInfo.class, id);
	}

	public void updateSummary(TSummaryInfo summaryInfo) {
		// TODO Auto-generated method stub
		hibernateDao.update(summaryInfo);
	}


	public List<Map<String, String>> selectDepartmentSummary(Integer userId, String dateInfo) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		sql.append("	SELECT department.*,table1.id summaryid,table1.createdate,                                                                                ");
		sql.append("	(SELECT systemuser.userName FROM dbo.t_system_user systemuser WHERE systemuser.userId = table1.createUserId)  AS createuser,  ");
		sql.append("    ( SELECT COUNT(*) FROM dbo.t_system_userDepartment  WHERE userId = ? AND departmentId = department.id) AS islocaldepment ");
		sql.append("	FROM dbo.t_system_department department                                                                               ");
		sql.append("	LEFT JOIN                                                                                                             ");
		sql.append("	(                                                                                                                     ");
		sql.append("	SELECT * FROM  dbo.t_summary_info info  WHERE info.infodate = ?                                                ");
		sql.append("	) AS table1 ON department.id = table1.createDepartmentId                                                              ");
		sql.append("	WHERE department.isDel = 1  AND (department.deptName <> '领导班子成员' AND department.deptName <> '粮食行业协会')     ");
		sql.append("                                                                                                                          ");
		sql.append("	 ORDER BY department.orderNo                                                                                          ");
		List<Map<String, String>> list = springDao.querySQL(sql.toString(), new Object[]{userId,dateInfo}, new RowMapper() {
			
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				Map<String, String> map = new HashMap<String, String>();
				map.put("deptName", arg0.getString("deptName"));
				if (arg0.getDate("createdate") != null) {
					map.put("createdatestr", ToolKit.FormatDate(arg0.getDate("createdate")));
				}				
				map.put("id", arg0.getString("id"));
				map.put("createuser", arg0.getString("createuser"));
				map.put("islocaldepment", arg0.getString("islocaldepment"));
				if (arg0.getInt("summaryid") == 0) {
					map.put("issumary", "未填写");
				} else {
					map.put("issumary", "已填写");
					map.put("summaryid", String.valueOf(arg0.getInt("summaryid")));
				}
				return map;
			}
		});
		return list;
	}

}
